package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.nio.file.WatchEvent;
import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends AppCompatActivity {

    private AppCompatTextView detailTextView;
    private RecyclerView detail_recyclerView;
    private DetailAdapter adapter;
    private Context context = this;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //实例类
        setContentView(R.layout.activity_ail);
        detailTextView = findViewById(R.id.detailTextView);
        //获取视图
        detail_recyclerView = findViewById(R.id.detail_RecyclerView);
        String city = getIntent().getStringExtra("city");

        OkGo.<String>get("https://tianqiapi.com/api?version=v6&appid=65716696&appsecret=pPQF7EgL" + city).execute(new StringCallback() {
            WeatherInfo.DataBean.HoursBean jsonDatas;

            @Override
            public void onSuccess(Response<String> response) {
                WeatherInfo weatherInfo = new Gson().fromJson(response.body(), WeatherInfo.class);
                List<WeatherInfo.DataBean> dataBeans = weatherInfo.getData();
                WeatherInfo.DataBean jsonData = dataBeans.get(1);

                detailTextView.setText("日期: " + jsonData.getDate() + '\n');
                detailTextView.append("天气: " + jsonData.getTem2() + "~" + jsonData.getTem1() + "," + jsonData.getWea() + '\n');
                detailTextView.append("风向: " + jsonData.getWin().get(0) + "," + jsonData.getWin_speed() + "," + jsonData.getWin_meter() + "\n");
                detailTextView.append("空气:" + jsonData.getAir_level() + "," + "PM2.5值为" + jsonData.getAir() + '\n');
                detailTextView.append("提示:" + jsonData.getAir_tips());

            }
        });


        OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=" + city).execute(new StringCallback() {
            private List<WeatherInfo.DataBean.HoursBean> hoursBeans;
            WeatherInfo.DataBean.HoursBean jsonData;

            @Override
            public void onSuccess(Response<String> response) {
                WeatherInfo weatherInfo = new Gson().fromJson(response.body(),WeatherInfo.class);
                List<WeatherInfo.DataBean> dataBeans = weatherInfo.getData();
                for (int i = 0; i < dataBeans.size(); i++) {
                    hoursBeans = weatherInfo.getData().get(i).getHours();
                    for (int j = 0; j < hoursBeans.size() ; j++) {
                        jsonData = hoursBeans.get(i);
                    }
                }

                adapter = new DetailAdapter((ArrayList<WeatherInfo.DataBean.HoursBean>) hoursBeans);
                detail_recyclerView.setLayoutManager(new LinearLayoutManager(context));
                detail_recyclerView.setAdapter(adapter);

            }
        });



    }
}
